[#ftl]
[@define "agile.WidgetStoreDialog" "Dialog" "Dialog=dijit/Dialog,Button=dijit/form/Button,Tree=dijit/Tree,Memory=dojo/store/Memory,Observable=dojo/store/Observable,ObjectStoreModel=dijit/tree/ObjectStoreModel"]
	title:"Beyond Prototype"
	
	,constructor:function(){
		aspect.after(this,"startup",lang.hitch(this,"init"));
	}
	,init:function(){
		
		this.containerNode.innerHTML = "<div class='tree'></div>";
		
		var widgetStore = new Memory({
			data:[{id:"root",title:"root"}]
			,getChildren:function(object){
				return this.query({parent:object.id});
			}
		});
		aspect.around(widgetStore,"put",function(originalPut){
			return function(obj,options){
				if(options && options.parent){
					obj.parent = options.parent.id;
				}
				return originalPut.call(widgetStore,obj,options);
			}
		});
		
		widgetStore = new Observable(widgetStore);
		
		var model = new ObjectStoreModel({
			store:widgetStore
			,query:{id:"root"}
			,labelAttr:"title"
		});
		
		
		var node = query(".tree",this.containerNode)[0];
		var tree = new Tree({model:model,autoExpand:true,showRoot:false
			,getTooltip:function(item){
				return agile.i18n("xxx",null,"Please double click to select the item");
			}
//			,onDblClick:function(item,node,evt){					
//				topic.publish("agile.object.selected",item);
//				dialog.hide();
//			}
		},node);
		
		tree.startup();
		
		this.subscribe("agile.object.lookup",function(context,callback){
			//clear the existing store
			array.forEach(widgetStore.query({parent:"root"}),function(item){
				widgetStore.remove(item.id);
			});
			//put the items to store to refresh the tree view
			array.forEach(callback.lookupItems.apply(context),function(item){
				widgetStore.put(item);
			});
			//double click to select an item and fire the callback
			var dialog = this;
			tree.onDblClick = function(item,node,evt){
				callback.onItemSelected.apply(context,[item]);
				dialog.hide();
			}
			this.show();
		});
	}
[/@]